/*
 * KeyboardInputTextBox.java
 *
 * Created on July 27, 2007, 10:07 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package org.mobileandembedded.idesktop;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;


import com.mar.meclient.bt.BTMidlet;
import com.mar.meclient.util.ui.IPage;
import com.mar.meclient.util.ui.IPageNavigator;

/**
 *
 * @author helio
 */
public class KeyboardInputTextBox extends TextBox implements CommandListener, IPage {
	private final IPageNavigator navigator;
	
    private final Command okCommand = new Command("OK", Command.SCREEN, 0);
    private final Command cancelCommand = new Command("Cancel", Command.EXIT, 0);
   
    private final ClientSideHandler clientHandler;
    /**
     * Creates a new instance of KeyboardInputTextBox
     *
     * @param remoteDesktopCanvas
     */
    public KeyboardInputTextBox(IPageNavigator navigator,  ClientSideHandler handler) {
        super("Keyboard Input", "", 1024, TextField.ANY);
        
        this.clientHandler = handler;
        this.navigator = navigator;
    }

    /**
     *
     * @param command
     * @param displayable
     */
    public void commandAction(Command command, Displayable displayable) {
        if (command == cancelCommand) {
        	navigator.navigateBack(this);
        } else if (command == okCommand) {
            try {
            	clientHandler.sendKeyboardInputMessage(getString());
            	clientHandler.sendRequestScreenshotMessage();
            	navigator.navigateBack(this);
            } catch (IOException ex) {
                ex.printStackTrace();
                BTMidlet.getInstance().showAlert(ex);
            }
        }
    }

	public void createPageContent(Object arg0) {
        addCommand(okCommand);
        addCommand(cancelCommand);
        setCommandListener(this);
	}

	public Displayable getDesplayable() {
		return this;
	}
}
